#!/usr/bin/python 
#-*- coding: utf-8 -*- 
#encoding=utf-8 
import urllib2 
import urllib 
import os 
import time
import sqlite3
from bs4 import BeautifulSoup 
def getAllImageLink():
	    number = 1
            for i in range(1,77): 
                html = urllib2.urlopen('http://www.dog126.com/buydog-p%s.html'%i,timeout=10).read() 
                soup = BeautifulSoup(html)
    # 需要数据：具体链接、雌雄、品种、价格、位置、具体链接
                dogli = soup.find_all('div',class_='dogli')
                for x in dogli:
                        # 具体链接，要使用的时候要进行拼装
                        message = x.find('a').get('href')
                        # 解析品种字段
                        gtip = x.find('li',class_='gtip').get_text().encode("utf8").split()
                        while '' in gtip:
                            gtip.remove('')
                        # 品种[0] 雌雄[1] 血统证书[2]
                        # 雌雄
                        sex = gtip[1].decode('utf8')[2:4].encode('utf8')
                        if sex == 'DD':
            		   sex =  'g'
            		else:
            		   sex =  'm'
                        # 品种
                        breed = gtip[0].decode('utf8')[3:].encode('utf8')
                        # 价格
                        price = x.find('em',class_='price').get_text().encode('utf8')
                        # 切片之后的价格str
                        price =  price.decode('utf8')[2:].encode('utf8')
                        price = int(float(price))
                        # 地区，还是应该用城市才对
                        area = x.find('li',class_='ln area').get_text().encode('utf8').split()
                        while '' in area:
                             area.remove('')
                        if len(area)>2:
                            city = area[2]
                        else:
                            city = 'none'
    		        cursor = con.cursor()
    		        cursor.execute("insert into info_mydog values('%d','%s','%s','%s','%s')"%(number,sex,breed,price,city))
    		        cursor.close()
    		        con.commit()
			print number
    		        number = number + 1
    		        print sex+breed+str(price)+city
                        print '----------------------------------'
            time.sleep(5)
                        # 三张图片
con = sqlite3.connect('db.sqlite3')
getAllImageLink()
con.close()
